package com.xianggu.aop;

import com.xianggu.XiangGuController;
import com.xianggu.spring.aop.config.Advice;
import com.xianggu.spring.aop.config.AopCglibProxyFactory;
import com.xianggu.spring.aop.joinPoint.ProceedingJoinPoint;
import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.Method;

/**
 * @author chengjy
 * @date 2025/3/10
 */
public class AopAdviceInterDemo implements Advice {

    public static void main(String[] args) {

        AopCglibProxyFactory adviceDemo = new AopCglibProxyFactory(new XiangGuController(), new AopAdviceInterDemo());

        Enhancer enhancer = new Enhancer();
        // 设置父类（目标类）
        enhancer.setSuperclass(XiangGuController.class);
        // 设置回调拦截器
        enhancer.setCallback(adviceDemo);

        XiangGuController proxy = (XiangGuController) enhancer.create();

        System.out.println(proxy.test("100000"));
    }
    /**
     * 返回需要切入的类全限定名
     *
     * @return
     */
    @Override
    public String Pointcut() {
        return "com.xianggu.aop.AopTest";
    }

    /**
     * 方法执行前
     *
     * @param joinPoint
     */
    @Override
    public void before(ProceedingJoinPoint joinPoint) {
        System.out.println(joinPoint.getMethod().getName() + "方法执行前");
    }

    /**
     * 方法执行后
     *
     * @param joinPoint
     */
    @Override
    public void after(ProceedingJoinPoint joinPoint) {
        System.out.println(joinPoint.getMethod().getName() + "方法执行后");
    }

    /**
     * 方法执行异常
     *
     * @param method
     * @param throwable
     */
    @Override
    public void afterThrowing(Method method, Throwable throwable) {
        System.out.println(method.getName() + "方法异常返回");
    }

    /**
     * 方法执行返回
     *
     * @param method
     * @param result
     */
    @Override
    public void afterReturning(Method method, Object result) {
        System.out.println(method.getName() + "方法执行返回");
    }

    /**
     * 方法执行around
     *
     * @param joinPoint
     */
    @Override
    public void around(ProceedingJoinPoint joinPoint) {

    }
}
